package com.zhang.family_doctor.job;

import com.zhang.family_doctor.entity.Packages;
import com.zhang.family_doctor.entity.SignUp;
import cn.hutool.core.date.DateUtil;
import com.zhang.family_doctor.mapper.PackagesMapper;
import com.zhang.family_doctor.mapper.SignUpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class SignUpJob {

    @Autowired
    private SignUpMapper signUpMapper;

    @Autowired
    private PackagesMapper packagesMapper;

    /**
     * 每天执行一次，每天晚上12点
     */
    @Scheduled(cron = "0 0 0 */1 * * ") //一年执行一次
    //@Scheduled(cron = "0/5 * * * * ?")  //五分钟执行一次
    public void dingShi() {
        List<SignUp> signUpList = signUpMapper.getList();
        for (SignUp signUp : signUpList) {
            if (signUp.getStatus() == 4) {
                Date expiryDate = DateUtil.date(signUp.getExpiryDate());
                long timestamp = System.currentTimeMillis();
                Date nowDateTime = DateUtil.date(timestamp);//获取系统当前时间
                if (nowDateTime.compareTo(expiryDate) >= 0) {
                    signUp.setStatus(5);
                    signUpMapper.updateById(signUp);
                } else {
                    System.out.println("正在定时执行签约过期！");
                }
            }
        }
    }

    /**
     * 每天执行一次，每天晚上12点
     */
    @Scheduled(cron = "0 0 0 */1 * * ") //一年执行一次
    public void renewalExecution(){
        List<SignUp> signUpList = signUpMapper.getList();
        signUpList.forEach(signUp -> {
            Integer renewalsStatus = signUp.getRenewalsStatus();
            long timestamp = System.currentTimeMillis();
            Date nowDateTime = DateUtil.date(timestamp);//获取系统当前时间
            Date renewalDate = signUp.getRenewalDate();
            Date expiryDate = signUp.getExpiryDate();
            //根据服务包id查询服务包
            Integer packagesId = signUp.getPackagesId();
            Packages packages = packagesMapper.getPackagesById(packagesId);
            //拿到服务包的签约周期
            Integer signingPeriod = packages.getSigningPeriod();

            if (renewalsStatus == 4) {
                if (nowDateTime.compareTo(expiryDate) >= 0) {
                    //生效日期
                    Date effectiveDate = signUp.getRenewalDate();

                    //签约周期分为1年、2年、3年
                    if (signingPeriod == 1){
                        //计算失效日期 一年
                        Calendar calendarOne = Calendar.getInstance();
                        calendarOne.setTime(effectiveDate);
                        calendarOne.add(calendarOne.YEAR, 1);
                        effectiveDate = calendarOne.getTime();

                    }else if (signingPeriod == 2){
                        //计算失效日期 两年
                        Calendar calendarTwo = Calendar.getInstance();
                        calendarTwo.setTime(effectiveDate);
                        calendarTwo.add(calendarTwo.YEAR, 2);
                        effectiveDate = calendarTwo.getTime();
                    }else if (signingPeriod == 3){
                        //计算失效日期 一年
                        Calendar calendarThree = Calendar.getInstance();
                        calendarThree.setTime(effectiveDate);
                        calendarThree.add(calendarThree.YEAR, 3);
                        effectiveDate = calendarThree.getTime();
                    }
                    signUp.setStatus(4);
                    signUp.setRenewalsStatus(5);
                    signUp.setEffectiveDate(renewalDate);
                    signUp.setExpiryDate(effectiveDate);
                    signUpMapper.renewalExecution(signUp);
                }else {
                    System.out.println("正在自动执行续约执行！");
                }
            }
        });

    }
}
